package com.atguigu.flink.chapter07;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.util.KKutil;
import org.apache.flink.api.common.eventtime.*;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.List;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/15 9:26
 */
public class Flink12_Watermark_transmit {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        env.setParallelism(3);

        env
                .socketTextStream("hadoop162", 9999)
                .map(line ->{
                    String[] strings = line.split(",");
                    return new WaterSensor(
                            strings[0],
                            Long.valueOf(strings[1]),
                            Integer.valueOf(strings[2]));
                })

                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                //为记录无序的情况创建水印策略，但您可以为事件无序的程度设置上限。 无序边界 B 意味着一旦遇到时间戳为 T 的事件，就不会再出现比T - B更早的事件。
                                //水印是周期性生成的。 这种水印策略引入的延迟是周期间隔长度，加上乱序界限。
                                .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner( (element, recordTimestamp) -> element.getTs())
                                // 避免数据倾斜导致的水印不更新
                                // 某个并行度水印超过5s不更新, 则使用其他的并行度的水印强制更新
                                .withIdleness(Duration.ofSeconds(5))
                )
                .keyBy(WaterSensor::getId)
                // 窗口时间间隔5s，5s一个窗口
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))

                .process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    @Override
                    public void process(String value, Context ctx, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {

                        List<WaterSensor> list = KKutil.toList(elements);
                        out.collect(value+"  "+ elements);

                    }
                })
                .setParallelism(2)

                .print();


        env.execute();
    }



}
